import { API } from "~/models/api";
import type { DataArrResponse, ResponseMsg } from "~/models/universal";
import type { UserProfileResponse } from "~/models/user";

export default defineNuxtPlugin(async (nuxtApp) => {
    const token = useCookie("token");
    if(!!token.value){
        
        var result = await $fetch<ResponseMsg<DataArrResponse<UserProfileResponse>>>("/api/user/querySelfProfile",{
            method:"GET",headers:authHeaders()
            
        })

        if(result.status && result.data.total >= 1){
            createSimpleElNotification(true,"用户自动登录成功...")
            useUserProfile().value = result.data.rows[0];
        }else{ 
            createSimpleElNotification(true,"用户自动登录失败...")
            token.value = null;
        }
    }
})
